package com.easylinkin.linkappapi.quality.controller;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.utils.async.AsyncUtil;
import com.easylinkin.linkappapi.quality.entity.ActualMeasureType;
import com.easylinkin.linkappapi.quality.entity.vo.ActualMeasureTypeVo;
import com.easylinkin.linkappapi.quality.service.ActualMeasureTypeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * ActualMeasureType表控制层
 *
 * @author zhengwen
 * @date 2022/08/30
 */
@Slf4j
@RestController
@RequestMapping("qualityActualMeasureType")
public class ActualMeasureTypeController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ActualMeasureTypeService actualMeasureTypeService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<ActualMeasureType> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ActualMeasureType> record = actualMeasureTypeService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.actualMeasureTypeService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appQualityActualMeasureType 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody ActualMeasureType appQualityActualMeasureType) {
        return RestBuilders.successBuilder().success((this.actualMeasureTypeService.saveOne(appQualityActualMeasureType))).build();
    }

    /**
     * 修改数据
     *
     * @param appQualityActualMeasureType 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody ActualMeasureType appQualityActualMeasureType) {
        return RestBuilders.successBuilder().success(this.actualMeasureTypeService.updateOne(appQualityActualMeasureType)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.actualMeasureTypeService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ActualMeasureType appQualityActualMeasureType, HttpServletRequest request, HttpServletResponse response) {
        actualMeasureTypeService.export(appQualityActualMeasureType, request, response);
    }

    /**
     * 初始化导入
     * @param file excel文件
     * @return 统一出参
     */
    @PostMapping(value = "/excelImport")
    @ResponseBody
    public RestMessage excelImport(@NotNull @RequestPart("file") MultipartFile file) {
        //获取当前时间的时间
        log.info("部位excel数据导入");
        String key = "qualityPosition:" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN);
        AsyncUtil.submitTask(key, () -> actualMeasureTypeService.excelImport(file));
        return RestBuilders.successBuilder().data(key).build();
    }

    /**
     * 企业级查询分页
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/enterpriseGetPage")
    @ApiOperation("企业级查询分页")
    public RestMessage enterpriseGetPage(@RequestBody RequestModel<ActualMeasureTypeVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        List<ActualMeasureType> record = actualMeasureTypeService.enterpriseGetPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 实测实量分类算法同步
     *
     * @return 删除结果
     */
    @PostMapping("/syncTypeItem")
    @ApiOperation("实测实量分类算法同步")
    public RestMessage syncTypeItem(@RequestBody ActualMeasureTypeVo actualMeasureTypeVo) {
        return actualMeasureTypeService.syncTypeItem(actualMeasureTypeVo);
    }

}

